package com.example.bishe.web.c.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.bishe.entity.Record;
import com.example.bishe.entity.User;
import com.example.bishe.service.RecordService;
import com.example.bishe.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  C端个人标签接口
 * </p>
 *
 * @author LDH
 * @since 2023/03/19
 */
@RestController
@Api(value = "小程序操作", tags = {"用户标签操作"})
@RequestMapping("/c/record")
@Slf4j
public class RecordController {

    @Autowired
    private RecordService recordService;

    @ApiOperation("分页查询用户一级记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "一级记录名称/描述", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "一级记录类型", paramType = "query"),
            @ApiImplicitParam(name = "show", value = "是否显示（0:否,1:是）", paramType = "query"),
    })
    @GetMapping("/page")
    public RestResult<IPage<Record>> page(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer show
    ){
        IPage<Record> recordIPage = recordService.getPage(pageNum, pageSize, userId, title, type, show);
        return RestResult.get_success(recordIPage);
    }

    @ApiOperation("用户添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标签标题", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "标签描述", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "标签类型", paramType = "query")
    })
    @PostMapping("/add")
    public RestResult<Void> add(@RequestBody Record record){
        try {
            recordService.add(record);
            return RestResult.set_success("添加成功");
        } catch (Exception e) {
            log.error("标签添加失败:[{}]",e.getMessage());
            return RestResult.set_success("添加失败");
        }
    }

    @ApiOperation("用户删除标签")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户标签id", paramType = "query")
    })
    @PostMapping("/delete")
    public RestResult<Void> delete(@RequestBody List<Long> ids){
        try {
            recordService.delete(ids);
            return RestResult.set_success("删除成功");
        } catch (Exception e) {
            log.error("标签删除失败:[{}]",e.getMessage());
            return RestResult.set_success("删除失败");
        }
    }
}
